package com.lancoo.znbkxx.teacher.ui.fragment

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.RadioGroup
import androidx.fragment.app.FragmentTransaction
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSID
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSNAME
import com.lancoo.znbkxx.teacher.Ext.Companion.GLOBAL_GRADE
import com.lancoo.znbkxx.teacher.Ext.Companion.INFOLIST
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.databinding.FmtReviewBinding
import com.lancoo.znbkxx.teacher.ui.activity.SearchActivity
import com.lancoo.znbkxx.teacher.ui.activity.SearchHistoryActivity
import com.lancoo.znbkxx.teacher.utils.MkUtils
import com.lancoo.znbkxx.teacher.utils.TimeUtils
import com.lancoo.znbkxx.teacher.viewmodel.UserInfoViewModel
import com.lancoo.znbkxx.teacher.widget.ClassMenu
import com.lancoo.znbkxx.teacher.widget.FilterPopupWindow

/**
 * @author : wealive
 * @date   : 2020/5/28 15:17
 * @desc   :
 */
class CommonReviewFragment : BaseVMFragment<UserInfoViewModel, FmtReviewBinding>(),
        View.OnClickListener, BaseQuickAdapter.OnItemClickListener,
        RadioGroup.OnCheckedChangeListener {
    private val mPopupWindow by lazy { FilterPopupWindow(requireContext(), this, this) }
    private val classPopupWindow by lazy {
        val list = mutableListOf<String>()
        INFOLIST!!.forEach {
            list.add(it.CourseClassName)
        }
        ClassMenu(requireContext(), list, this)
    }
    private var state: String = "-1"
    private var lowDateTime = 0L
    private var highDateTime = 0L
    private var position: Int = -1
    val layer by lazy { arguments?.getInt("layer") }
    private val basicReviewFragment by lazy { BasicReviewFragment.newInstance(layer!!) }

    companion object {
        private const val LAYER = "layer"
        fun newInstance(layer: Int): CommonReviewFragment {
            val fragment = CommonReviewFragment()
            val bundle = Bundle()
            bundle.putInt(LAYER, layer)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun setLayoutID(): Int {
        return R.layout.fmt_review
    }


    override fun initView() {
        binding.model = viewModel
        binding.tvSearch.setOnClickListener(this)
        binding.ibnSearch.setOnClickListener(this)
        initSpinner()
        initFragment()
    }

    private fun initSpinner() {
        binding.edtSearch.isFocusable = false
        binding.edtSearch.setOnClickListener {
            startActivity(
                Intent(requireContext(), SearchHistoryActivity::class.java).putExtra("layer",
                    layer.toString()))
        }
        binding.tvClassname.text = COURSE_CLASSNAME
        binding.tvClassname.setOnClickListener(this)
        INFOLIST?.size?.let {
            if (it > 1) {
                binding.ivSwitch.visibility = View.VISIBLE
                binding.ivSwitch.setOnClickListener(this)
            } else {
                binding.ivSwitch.visibility = View.INVISIBLE
            }
        }
    }

    private fun initFragment() {
        val fmt = childFragmentManager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        if (basicReviewFragment.isAdded) {
            fmt.show(basicReviewFragment).commit()
        } else {
            fmt.add(R.id.fl, basicReviewFragment).commitNow()
        }
    }

    override fun providerContext(): Any? {
        return activity
    }

    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_classname, R.id.iv_switch -> {
                classPopupWindow.setOnDismissListener {
                    val lp = requireActivity().window.attributes
                    lp.alpha = 1.0f
                    requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
                    requireActivity().window.attributes = lp
                }
                classPopupWindow.showAsDropDown(binding.tvClassname, 0, 10)
                val lp = requireActivity().window.attributes
                lp.alpha = 0.8f
                requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
                requireActivity().window.attributes = lp
            }
            R.id.tv_search, R.id.ibn_search -> {
                reSet()
                state = "-1"
                //                date = "0"
                lowDateTime = 0L
                highDateTime = 0L
                mPopupWindow.showAsDropDown(binding.cons, 0, 10)
            }
            R.id.iv_reset -> {
                reSet()
                state = "-1"
                //                date = "0"
                lowDateTime = 0L
                highDateTime = 0L
            }
            R.id.iv_config -> {
                mPopupWindow.dismiss()
                startActivity(Intent(activity, SearchActivity::class.java).putExtra("state", state)
                    .putExtra("lowDateTime", lowDateTime).putExtra("highDateTime", highDateTime)
                    .putExtra("position", position).putExtra("layer", layer.toString()))
            }
        }
    }

    fun setClass(name: String) {
        binding.tvClassname.text = name
        basicReviewFragment.initPaper()
    }

    private fun reSet() {
        mPopupWindow.contentView.findViewById<RadioGroup>(R.id.rg_wcqk).clearCheck()
        mPopupWindow.contentView.findViewById<RadioGroup>(R.id.rg_zdsj).clearCheck()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        COURSE_CLASSNAME = INFOLIST!![position].CourseClassName
        MkUtils.encode("COURSE_CLASSNAME",COURSE_CLASSNAME)
        COURSE_CLASSID = INFOLIST!![position].CourseClassID
        MkUtils.encode("COURSE_CLASSID",COURSE_CLASSID)
        GLOBAL_GRADE = INFOLIST!![position].GlobalGrade
        MkUtils.encode("GLOBAL_GRADE",GLOBAL_GRADE)
        for (i in 0..1) {
            (parentFragmentManager.findFragmentByTag("f$i") as CommonReviewFragment).setClass(
                COURSE_CLASSNAME)
        }
        val paperFollowingFragment = parentFragmentManager.findFragmentByTag("f2") as PaperFollowingFragment
        paperFollowingFragment.setClass(COURSE_CLASSNAME)
        val commentsFragment = parentFragmentManager.findFragmentByTag("f3") as CommentsFragment
        commentsFragment.setClass(COURSE_CLASSNAME,position)
        val cognitiveDiagnosisFragment = parentFragmentManager.findFragmentByTag("f4") as CognitiveDiagnosisFragment
        //        cognitiveDiagnosisFragment.setClass()
        classPopupWindow.dismiss()
    }

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.tv_2_1 -> state = "-1"
            R.id.tv_2_2 -> state = "0|1"
            R.id.tv_2_3 -> state = "2|3"
            R.id.tv_2_4 -> state = "4"
            R.id.tv_3_1 -> {
                //                date = TimeUtils.convert2Minute(0, 0, 0) + "|" + TimeUtils.convert2Minute(-7, 0, 0)
                lowDateTime = TimeUtils.convert2Second(-7, 0, 0)
                highDateTime = TimeUtils.convert2Second(0, 0, 0)
                position = 0
            }
            R.id.tv_3_2 -> {
                //                date = TimeUtils.convert2Minute(0, 0, 0) + "|" + TimeUtils.convert2Minute(-14, 0, 0)
                lowDateTime = TimeUtils.convert2Second(-14, 0, 0)
                highDateTime = TimeUtils.convert2Second(0, 0, 0)
                position = 1
            }
            R.id.tv_3_3 -> {
                //                date = TimeUtils.convert2Minute(0, 0, 0) + "|" + TimeUtils.convert2Minute(0, -1, 0)
                lowDateTime = TimeUtils.convert2Second(0, -1, 0)
                highDateTime = TimeUtils.convert2Second(0, 0, 0)
                position = 2
            }
            R.id.tv_3_4 -> {
                //                date = TimeUtils.convert2Minute(0, -1, 0) + "|" + "2018.01.01 00:00"
                lowDateTime = 0L
                highDateTime = TimeUtils.convert2Second(0, -1, 0)
                position = 3
            }
        }
    }
}